import { isLogin } from '@/api/common';
import { userInfo as getUserInfo } from '@/api/interface/interface';
import type { UserInfo } from '@/api/type/interface';
import { defineStore } from 'pinia';

/**
 * 存入参数
 */
export const userStore = defineStore('user', {
    persist: {
        enabled: true,
        // strategies: [{ key: 'user', storage: localStorage }]
    },
    state: () => {  // 存放的就是模块的变量
        return {
            userInfo: {} as UserInfo,
        }
    },
    actions: {
        setUserInfo(data: any) {
            this.userInfo = data;
        },
        updateUserInfo: async () => {
            if (!isLogin()) {
                console.log("请先登录");
                return;
            }
            const { data } = await getUserInfo();
            const { setUserInfo } = userStore();
            setUserInfo(data);
        },
        clearUserInfo(data: any) {
            console.log('清空')
            this.userInfo = data;
        }
    }
});
